package com.itheima.controller;

import com.itheima.domain.db.Questionnaire;
import com.itheima.domain.db.SoulQuestion;
import com.itheima.domain.db.SoulQuestionOption;
import com.itheima.manager.QuestionnaireManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
public class QuestionnaireController {

    @Autowired

    private QuestionnaireManager questionnaireManager;

    // 查询分页列表
    @ResponseBody
    @RequestMapping("/movies/findByPage")
    public PageBeanVo findQuestionnaireList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        PageBeanVo pageBeanVo = questionnaireManager.findQuestionnaireList(pageNum, pageSize);
        List<Questionnaire> items = (List<Questionnaire>) pageBeanVo.getItems();
        for (Questionnaire item : items) {
            System.out.println(item);
        }

        return pageBeanVo;
    }

    // 根据问卷等级查询该问卷的所有问题
    @RequestMapping("/question/findById")
    @ResponseBody
    public PageBeanVo findQuestionListById(Integer questionnaireId,
                                           @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        PageBeanVo pageBeanVo = questionnaireManager.findQuestionListById(questionnaireId,pageNum, pageSize);

        List<SoulQuestion> items = (List<SoulQuestion>) pageBeanVo.getItems();
        for (SoulQuestion item : items) {
            System.out.println(item);
        }
        return pageBeanVo;
    }

    // 根据id删除问题
    @ResponseBody
    @RequestMapping("/question/deleteById")
    public void deleteQuestionById(Long id){
        questionnaireManager.deleteQuestionById(id);
    }

    // 添加问题
    /*@RequestMapping("/question/save")
    public String saveQuestion(String id,String question,String optionA,String optionB,String scoreA,String scoreB,String optionC,String optionD,String scoreC,String scoreD){
        // 保存问题以及选项
        questionnaireManager.saveQuestion(id,question,optionA,optionB,scoreA,scoreB,optionC,optionD,scoreC,scoreD);
        return "redirect:/soul_question.html?id="+id;
    }*/
    @RequestMapping("/question/save")
    public String saveQuestion(String id,String question,String[] option,Integer[] score,Integer[] extroversion,Integer[] judgement,Integer[] abstraction,Integer[] rationality){
        // 保存问题以及选项
        questionnaireManager.saveQuestion(id,question,option,score,extroversion,judgement,abstraction,rationality);
        return "redirect:/soul_question.html?id="+id;
    }

    @RequestMapping("/question/findQuestionOptionById")
    @ResponseBody
    public PageBeanVo findQuestionOptionById(Integer questionId,
                                             @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        PageBeanVo pageBeanVo = questionnaireManager.findQuestionOptionById(questionId, pageNum, pageSize);
        List<SoulQuestionOption> items = (List<SoulQuestionOption>) pageBeanVo.getItems();
        for (SoulQuestionOption item : items) {
            System.out.println(item);
        }
        return pageBeanVo;
    }

    // 问题选项数据回显
    @RequestMapping("/question/findQuestionOptionInfoById")
    @ResponseBody
    public SoulQuestionOption findQuestionOptionInfoById(Long questionQptionId){
        return questionnaireManager.findQuestionOptionInfoById(questionQptionId);
    }

    // 修改问题的 选项以及分数
    @RequestMapping("/questionOptionInfo/save")
    public String saveQuestionOptionInfo(Long id,Long questionId, String content,Integer score){
        questionnaireManager.saveQuestionOptionInfo(id,content,score);
        return "redirect:/soul_question_update.html?questionId="+questionId;
    }

    // 删除问题的选项(根据选项id)
    @RequestMapping("/question/deleteQuestionOptionById")
    @ResponseBody
    public void  deleteQuestionOptionById(Long questionOptionId) {
        questionnaireManager.deleteQuestionOptionById(questionOptionId);
    }

    // 添加问题的选项
    @RequestMapping("/questionOption/save")
    public String saveQuestionOption(SoulQuestionOption soulQuestionOption){
        questionnaireManager.saveQuestionOption(soulQuestionOption);
        return "redirect:/soul_question_update.html?questionId="+soulQuestionOption.getQuestionId();
    }


}
